package com.hsbc.dev.teamo4.sms.sm.dao.impl;
import com.hsbc.dev.teamo4.sms.common.utils.factory.DBConnectFactory;
import com.hsbc.dev.teamo4.sms.common.utils.factory.UUIDFactory;
import com.hsbc.dev.teamo4.sms.sm.dao.IDictionaryDao;
import com.hsbc.dev.teamo4.sms.sm.po.Dictionary;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;

/**
 * @Description: dao接口实现
 * @author: Young
 * @version: 1.0
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.dao.impl
 * @Date date: 2018/7/26
 */
public class DictionaryDaoImpl implements IDictionaryDao {

    private QueryRunner queryRunner = DBConnectFactory.getQueryRunner();

    /**
     * @Description: 插入字典记录。
     * @Param: [dictionary]
     * @return: com.hsbc.dev.teamo4.sms.sm.po.Dictionary
     * @Author: Young
     * @Date: 2018/7/28
     */
    @Override
    public Dictionary saveEntity(Dictionary dictionary) {
        String sql = "insert into sm_dictionary(id,dictionaryName,dictionaryContent,createId,createTime,updateId,updateTime,isEnable) values(?,?,?,?,?,?,?,?)";
        int row = 0;
        try {
            row = queryRunner.update(sql, UUIDFactory.getUUID(), dictionary.getDictionaryName(), dictionary.getDictionaryContent(), dictionary.getCreateId(), dictionary.getCreateTime(), dictionary.getUpdateId(), dictionary.getUpdateTime(), dictionary.getIsEnable());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (row>0){
            return dictionary;
        }
        return null;
    }

    /**
     * @Description: 更新字典记录。
     * @Param: [dictionary]
     * @return: com.hsbc.dev.teamo4.sms.sm.po.Dictionary
     * @Author: Young
     * @Date: 2018/7/28
     */
    @Override
    public Dictionary updateEntity(Dictionary dictionary) {
        String sql = "update sm_dictionary set dictionaryName = ?,dictionaryContent = ?,createId = ?,createTime = ?,updateId = ?,updateTime = ?,isEnable = ? where id = ?";
        int row = 0;
        try {
            row = queryRunner.update(sql, dictionary.getDictionaryName(), dictionary.getDictionaryContent(), dictionary.getCreateId(), dictionary.getCreateTime(), dictionary.getUpdateId(), dictionary.getUpdateTime(), dictionary.getIsEnable(),dictionary.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (row>0){
            return dictionary;
        }
        return null;
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        return 0;
    }

    /**
     * @Description: 根据id查询字典记录。
     * @Param: [id]
     * @return: com.hsbc.dev.teamo4.sms.sm.po.Dictionary
     * @Author: Young
     * @Date: 2018/7/28
     */
    @Override
    public Dictionary queryById(String id) {
        String sql = "select id,dictionaryName,dictionaryContent,createId,createTime,updateId,updateTime,isEnable from sm_dictionary where id = ? and isEnable = true";
        Dictionary dictionary = null;
        try {
            dictionary = queryRunner.query(sql, new BeanHandler<Dictionary>(Dictionary.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dictionary;
    }

    /**
     * @Description: 查询所有的字典记录。
     * @Param: []
     * @return: java.util.List<com.hsbc.dev.teamo4.sms.sm.po.Dictionary>
     * @Author: Young
     * @Date: 2018/7/28
     */
    @Override
    public List<Dictionary> queryAllEntity() {
        String sql = "select id,dictionaryName,dictionaryContent,createId,createTime,updateId,updateTime,isEnable from sm_dictionary where isEnable = true";
        List<Dictionary> list  = null;
        try {
            list = queryRunner.query(sql, new BeanListHandler<Dictionary>(Dictionary.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @Description: 根据字典名称查询出所有匹配的记录。
     * @Param: [dictionaryName]
     * @return: java.util.List<com.hsbc.dev.teamo4.sms.sm.po.Dictionary>
     * @Author: Young
     * @Date: 2018/7/28
     */
    @Override
    public List<Dictionary> queryListByName(String dictionaryName) {
        String sql = "select id,dictionaryName,dictionaryContent,createId,createTime,updateId,updateTime,isEnable from sm_dictionary where isEnable = true and dictionaryName = ?";
        List<Dictionary> list  = null;
        try {
            list = queryRunner.query(sql, new BeanListHandler<Dictionary>(Dictionary.class),dictionaryName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @Description: 根据id对记录进行逻辑删除 。
     * @Param: [id]
     * @return: boolean
     * @Author: Young
     * @Date: 2018/7/28
     */
    @Override
    public boolean deleteById(String id) {
        String sql = "update sm_dictionary set isEnable = false where id = ?";
        int row = 0;
        try {
            row = queryRunner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (row>0){
            return true;
        }
        return false;
    }
}
